package com.example.day6_viewpager;

import androidx.appcompat.app.AppCompatActivity;
import androidx.fragment.app.Fragment;
import androidx.viewpager.widget.ViewPager;

import android.os.Bundle;
import android.view.ViewPropertyAnimator;
import android.widget.Button;
import android.widget.ListView;
import android.widget.RadioGroup;

import com.example.day6_viewpager.adapter.MyPagerAdapter;
import com.example.day6_viewpager.fragment.OneFragment;
import com.example.day6_viewpager.fragment.ThreeFragment;
import com.example.day6_viewpager.fragment.TwoFragment;

import java.util.ArrayList;
import java.util.List;

/***
 viewpager+fragment结合使用（*****）
 案例一：实现fragment切换
 案例二：实现引导页
 案例三：实现轮播图，自动轮播


 实现流程：使用适配器完成
 1。listview------》ViewPager
 2。BaseAdapter-----》FragmentPagerAdapter
 3。List<实体类>-----》List<Fragment>
 4.上下联动




 */
public class MainActivity extends AppCompatActivity {
    private ViewPager viewPager;
    private MyPagerAdapter myPagerAdapter;
    private List<Fragment> list;

    private RadioGroup radioGroup;


    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        viewPager = findViewById(R.id.view_pager);
        list = new ArrayList<>();
        list.add(new OneFragment());
        list.add(new TwoFragment());
        list.add(new ThreeFragment());
        myPagerAdapter = new MyPagerAdapter(getSupportFragmentManager(),list);
        viewPager.setAdapter(myPagerAdapter);

        //点击底部切换fragment
        radioGroup = findViewById(R.id.group);
        radioGroup.setOnCheckedChangeListener(new RadioGroup.OnCheckedChangeListener() {
            @Override
            public void onCheckedChanged(RadioGroup group, int checkedId) {
                switch (checkedId){
                    case R.id.rb1:
                        viewPager.setCurrentItem(0);
                        break;
                    case R.id.rb2:
                        viewPager.setCurrentItem(1);
                        break;
                    case R.id.rb3:
                        viewPager.setCurrentItem(2);
                        break;

                }
            }
        });
        //滑动fragment，底部切换
        viewPager.addOnPageChangeListener(new ViewPager.OnPageChangeListener() {
            @Override
            public void onPageScrolled(int position, float positionOffset, int positionOffsetPixels) {
            }

            @Override
            public void onPageSelected(int position) {//选好了 0 1 2
                switch (position){
                    case 0:
                        radioGroup.check(R.id.rb1);
                        break;
                    case 1:
                        radioGroup.check(R.id.rb2);
                        break;
                    case 2:
                        radioGroup.check(R.id.rb3);
                        break;
                }

            }

            @Override
            public void onPageScrollStateChanged(int state) {

            }
        });


    }
}
